package cn.beforedark.y24.m09.d18;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class Main1 {
    public static void main(String[] args) {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                60L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                new ThreadFactory() {
                    private final AtomicInteger threadNumber = new AtomicInteger(1);

                    @Override
                    public Thread newThread(Runnable r) {
                        Thread t = new Thread(r, "我定义的线程-" + threadNumber.getAndIncrement());
                        return t;
                    }
                },
                // 默认拒绝策略
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );


        Runnable runnable = () -> {
            log.info("任务开始执行");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                log.info("线程睡着的时候抛了异常");
                throw new RuntimeException(e);
            }finally {
                log.info("任务结束执行");
            }
        };


        for (int i = 0; i < 20; i++) {
            threadPoolExecutor.execute(runnable);
        }
    }
}
